package com.memorypalace.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.memorypalace.biz.IKnowledgeTreeBiz;
import com.memorypalace.biz.INoteConnectBiz;
import com.memorypalace.deck.biz.IDeckBiz;
import com.memorypalace.deck.biz.INoteBiz;
import com.memorypalace.deck.vo.NoteQueryPageBean;
import com.memorypalace.knowledge.pojo.KnowledgeTree;
import com.memorypalace.knowledge.pojo.Node;
import com.memorypalace.knowledge.pojo.NoteConnect;
import com.memorypalace.pojo.Deck;
import com.memorypalace.pojo.Note;
import com.memorypalace.pojo.User;
import com.memorypalace.util.AJaxUtil;

@Controller
@RequestMapping("/knowledge")
public class KnowledgeAction {

	@Autowired
	private IKnowledgeTreeBiz knowledgeTreeBiz;
	@Autowired
	private INoteConnectBiz noteConnectBiz;
	@Autowired
	private IDeckBiz deckBiz;
	@Autowired
	private INoteBiz noteBiz;

	@RequestMapping("/main")
	public String toKnowledgePage(HttpServletRequest request) {
		System.out.println("toKnowledgePage");
		String userId = "c34487b4-35e3-11e7-8e4c-e18a12b866d4";
		User user = (User) request.getSession().getAttribute("user");
		userId = user.getUserId();
		// deckBiz.getDeck_Note_NoteType_CardStyle_CardByUserId(userId);
		// deckBiz.getDeck_Note_NoteType_CardStyle_CardByStudyListId(null);
		// 加了斜杠,访问的网址是 /memorypalace/myknowledgetree.html
		return "myknowledgetree.html";
	}

	@RequestMapping("/getknowledgetree")
	@ResponseBody
	public void getKnowledgeTree(HttpServletRequest request,HttpServletResponse response,String knowledgeId) {
		System.out.println("getKnowledgeTree!");
		String userId = "c34487b4-35e3-11e7-8e4c-e18a12b866d4";
		User user = (User) request.getSession().getAttribute("user");
		userId = user.getUserId();
		List<Node> nodes = new ArrayList<Node>();
		Node n = new Node();
		n.setTopic("我的知识体系");
		n.setId("root");
		n.setIsroot(true);
		n.getData().put("type", 1);
		nodes.add(n);
		nodes.addAll(knowledgeTreeBiz.getNodesByUsersKnowledgeTree(userId,knowledgeId));
		System.out.println("nodes:"+nodes);
		AJaxUtil.sendJsonByObj(nodes, response);
	}

	@RequestMapping("/getnotes")
	@ResponseBody
	public void getNotesByKnowledgeTree(HttpServletResponse response, String knowledgeTreeId) {
		System.out.println("knowledgeTreeId:" + knowledgeTreeId);
		// 先
		List<Note> notes = noteConnectBiz.getNotesByFatherKnowledgeId(knowledgeTreeId);
		AJaxUtil.sendJsonByObj(notes, response);
	}

	/**
	 * 
	 * @param response
	 * @param deckId
	 *            要查找笔记的牌组id,如果为空,则查找所有deckId
	 * @param pageIndex
	 *            要查找第几页的内容,如果为空则查第1页的内容
	 */
	@RequestMapping("/toaddknowledgetree")
	@ResponseBody
	public void toAddKnowledgeTree(HttpServletRequest request,HttpServletResponse response, NoteQueryPageBean noteInPage) {
		String userId = "c34487b4-35e3-11e7-8e4c-e18a12b866d4";
		User user = (User) request.getSession().getAttribute("user");
		userId = user.getUserId();
		List<Note> noteList = new ArrayList<Note>();
		System.out.println("searchText:"+noteInPage.getSearchText());
		NoteQueryPageBean noteInPage2 = null;
		if (noteInPage.getDeckId() == null || noteInPage.getDeckId().trim().length() == 0) {
			noteInPage2 = noteBiz.getNoteQueryPageBeanByUserId(userId, noteInPage);
		} else {
			// noteList = noteBiz.getNotesByDeckId(deckId);
			noteInPage2 = noteBiz.getNoteQueryPageBeanByNoteQueryPageBean(noteInPage);
			if(noteInPage2 != null){
				noteInPage2.setCurPage(noteInPage.getCurPage());
				noteInPage2.setPageCount(noteInPage.getPageCount());
				System.out.println("新的:" + noteInPage2);
			}
			// noteList = noteInPage2.getData();
		}
		if (noteInPage2 != null) {
			AJaxUtil.sendJsonByObj(noteInPage2, response);
		} else {
			AJaxUtil.sendJsonByObj(noteInPage, response);
		}
	}

	/**
	 * 
	 * @param response
	 */
	@RequestMapping("/getmydeck")
	@ResponseBody
	public void getmydeck(HttpServletRequest request,HttpServletResponse response) {
		String userId = "c34487b4-35e3-11e7-8e4c-e18a12b866d4";
		User user = (User) request.getSession().getAttribute("user");
		userId = user.getUserId();
		List<Deck> deckList = new ArrayList<Deck>();
		deckList = deckBiz.getDecksByUserId(userId);
		AJaxUtil.sendJsonByObj(deckList, response);
	}

	/**
	 * @param response
	 * @param addKnowledgeJSON
	 *            字符串数组,第一个字符串为KnowledgeTree,后面为noteId
	 */
	@RequestMapping("/afteraddknowledgetree")
	@ResponseBody
	public void afteraddknowledgetree(HttpServletRequest request,HttpServletResponse response, KnowledgeTree knowledgeTree, String[] noteIds) {
		System.out.println("knowledge:" + knowledgeTree);
		System.out.println("noteIds:" + noteIds.length);
		String userId = "c34487b4-35e3-11e7-8e4c-e18a12b866d4";
		User user = (User) request.getSession().getAttribute("user");
		userId = user.getUserId();
		knowledgeTree.setUserId(userId);
		if("root".equals(knowledgeTree.getFatherKnowledgeId())){
			knowledgeTree.setFatherKnowledgeId(null);
		}
		Integer result = knowledgeTreeBiz.addKnowledgeTree(knowledgeTree);

		System.out.println("result:" + result);
		System.out.println("knowledgeId:" + knowledgeTree.getKnowledgeId());
		for (String noteId : noteIds) {
			NoteConnect noteConnect = new NoteConnect();
			noteConnect.setNoteId(noteId);
			noteConnect.setFatherKnowledgeId(knowledgeTree.getKnowledgeId());
			noteConnectBiz.addNoteConnect(noteConnect);
		}
	}

	/**
	 * @param response
	 * @param addKnowledgeJSON
	 *            字符串数组,第一个字符串为KnowledgeTree,后面为noteId
	 */
	@RequestMapping("/toeditknowledgetree")
	@ResponseBody
	public void toeditknowledgetree(HttpServletRequest request,HttpServletResponse response, String knowledgeTreeId) {
		System.out.println("toeditknowledgetree knowledgeTreeId:" + knowledgeTreeId);
		KnowledgeTree knowledgeTree = knowledgeTreeBiz.getKnowledgeTree(knowledgeTreeId);
		System.out.println("knowledgeTree:" + knowledgeTree);
		AJaxUtil.sendJsonByObj(knowledgeTree, response);
	}

	/**
	 * @param response
	 * @param addKnowledgeJSON
	 *            字符串数组,第一个字符串为KnowledgeTree,后面为noteId
	 */
	@RequestMapping("/aftereditknowledgetree")
	@ResponseBody
	public void aftereditknowledgetree(HttpServletRequest request,HttpServletResponse response, KnowledgeTree knowledgeTree, String[] noteIds) {
		System.out.println("knowledge:" + knowledgeTree);
		System.out.println("noteIds:" + noteIds.length);
		String userId = "c34487b4-35e3-11e7-8e4c-e18a12b866d4";
		User user = (User) request.getSession().getAttribute("user");
		userId = user.getUserId();
		knowledgeTree.setUserId(userId);
		// 编辑knowledgeTree表
		if("root".equals(knowledgeTree.getFatherKnowledgeId())){
			knowledgeTree.setFatherKnowledgeId(null);
		}
		Integer result = knowledgeTreeBiz.updateKnowledgeTree(knowledgeTree);
		// 删除noteConnect表where fatherKnowledgeId=当前
		Integer deleteResult = noteConnectBiz.deleteNoteConnectsByFatherKnowledgeId(knowledgeTree.getKnowledgeId());
		// 插入noteConnect表
		System.out.println("result:" + result);
		System.out.println("deleteResult:" + result);
		System.out.println("knowledgeId:" + knowledgeTree.getKnowledgeId());
		for (String noteId : noteIds) {
			NoteConnect noteConnect = new NoteConnect();
			noteConnect.setNoteId(noteId);
			noteConnect.setFatherKnowledgeId(knowledgeTree.getKnowledgeId());
			noteConnectBiz.addNoteConnect(noteConnect);
		}
	}

	/**
	 * @param response
	 * @param noteId
	 *            要删除的knowledgeID
	 */
	@RequestMapping("/deleteknowledgetree")
	@ResponseBody
	public void deleteknowledgetree(HttpServletResponse response, String knowledgeId) {
		System.out.println("noteId:" + knowledgeId);
		// 删除该知识体系下所有的和笔记的连接
		noteConnectBiz.deleteNoteConnectsByFatherKnowledgeId(knowledgeId);
		
		// 删除当前知识体系
		Integer result = knowledgeTreeBiz.deleteKnowledgeTree(knowledgeId);
	}
}
